{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Welcome to the Glue Interactive Sessions Kernel\n",
      "For more information on available magic commands, please type %help in any new cell.\n",
      "\n",
      "It looks like there is a newer version of the kernel available. The latest version is 0.30 and you have 0.24 installed.\n",
      "Please run `pip install --upgrade aws-glue-sessions` to upgrade your kernel\n",
      "Current iam_role is None\n",
      "iam_role has been set to arn:aws:iam::079002598131:role/svfw-z-us-east-1-sagemaker-execution-role.\n",
      "Previous region: None\n",
      "Setting new region to: us-east-1\n",
      "Region is set to: us-east-1\n",
      "Setting session ID prefix to 112233445566\n"
     ]
    }
   ],
   "source": [
    "%iam_role 'arn:aws:iam::079002598131:role/svfw-z-us-east-1-sagemaker-execution-role'\n",
    "%region 'us-east-1'\n",
    "%session_id_prefix '112233445566'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Authenticating with environment variables and user-defined glue_role_arn: arn:aws:iam::079002598131:role/svfw-z-us-east-1-sagemaker-execution-role\n",
      "Attempting to use existing AssumeRole session credentials.\n",
      "Trying to create a Glue session for the kernel.\n",
      "Worker Type: G.1X\n",
      "Number of Workers: 5\n",
      "Session ID: 112233445566-aa50d0d0-2ca1-4d38-97e1-93b7b785ce67\n",
      "Applying the following default arguments:\n",
      "--glue_kernel_version 0.24\n",
      "--enable-glue-datacatalog true\n",
      "Waiting for session 112233445566-aa50d0d0-2ca1-4d38-97e1-93b7b785ce67 to get into ready status...\n",
      "Session 112233445566-aa50d0d0-2ca1-4d38-97e1-93b7b785ce67 has been created\n",
      "\n",
      "<pyspark.sql.session.SparkSession object at 0x7f675aa0c2d0>\n"
     ]
    }
   ],
   "source": [
    "spark"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "customer_reviews = spark.read.format('csv') \\\n",
    "                            .option('header', 'true') \\\n",
    "                            .option('inferSchema', 'true') \\\n",
    "                            .option('sep', '\\t') \\\n",
    "                            .load('s3://dsoaws/amazon_reviews_us_Digital_Software_v1_00.tsv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "customer_reviews.registerTempTable('customer_reviews')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception encountered while retrieving session: An error occurred (ExpiredTokenException) when calling the GetSession operation: The security token included in the request is expired \n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.7/site-packages/aws_glue_interactive_sessions_kernel/glue_python_kernel/GlueKernel.py\", line 619, in get_current_session\n",
      "    current_session = self.glue_client.get_session(Id=self.get_session_id())[\"Session\"]\n",
      "  File \"/opt/conda/lib/python3.7/site-packages/botocore/client.py\", line 415, in _api_call\n",
      "    return self._make_api_call(operation_name, kwargs)\n",
      "  File \"/opt/conda/lib/python3.7/site-packages/botocore/client.py\", line 745, in _make_api_call\n",
      "    raise error_class(parsed_response, operation_name)\n",
      "botocore.exceptions.ClientError: An error occurred (ExpiredTokenException) when calling the GetSession operation: The security token included in the request is expired\n",
      "Failed to retrieve session status \n",
      "\n",
      "Exception encountered while running statement: An error occurred (ExpiredTokenException) when calling the RunStatement operation: The security token included in the request is expired \n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.7/site-packages/aws_glue_interactive_sessions_kernel/glue_python_kernel/GlueKernel.py\", line 103, in do_execute\n",
      "    statement_id = self.glue_client.run_statement(SessionId=self.get_session_id(), Code=code)[\"Id\"]\n",
      "  File \"/opt/conda/lib/python3.7/site-packages/botocore/client.py\", line 415, in _api_call\n",
      "    return self._make_api_call(operation_name, kwargs)\n",
      "  File \"/opt/conda/lib/python3.7/site-packages/botocore/client.py\", line 745, in _make_api_call\n",
      "    raise error_class(parsed_response, operation_name)\n",
      "botocore.exceptions.ClientError: An error occurred (ExpiredTokenException) when calling the RunStatement operation: The security token included in the request is expired\n"
     ]
    }
   ],
   "source": [
    "one_record = spark.sql('select * from customer_reviews limit 1')\n",
    "one_record.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ParseException: \"\\nmismatched input '<EOF>' expecting {'(', 'SELECT', 'FROM', 'ADD', 'DESC', 'WITH', 'VALUES', 'CREATE', 'TABLE', 'INSERT', 'DELETE', 'DESCRIBE', 'EXPLAIN', 'SHOW', 'USE', 'DROP', 'ALTER', 'MAP', 'SET', 'RESET', 'START', 'COMMIT', 'ROLLBACK', 'REDUCE', 'REFRESH', 'CLEAR', 'CACHE', 'UNCACHE', 'DFS', 'TRUNCATE', 'ANALYZE', 'LIST', 'REVOKE', 'GRANT', 'LOCK', 'UNLOCK', 'MSCK', 'EXPORT', 'IMPORT', 'LOAD'}(line 1, pos 0)\\n\\n== SQL ==\\n\\n^^^\\n\"\n"
     ]
    }
   ],
   "source": [
    "%%sql select * from customer_reviews limit 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "instance_type": "ml.t3.medium",
  "kernelspec": {
   "display_name": "Python 3 (Data Science)",
   "language": "python",
   "name": "python3__SAGEMAKER_INTERNAL__arn:aws:sagemaker:us-east-1:081325390199:image/datascience-1.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
